package utils
{
	import flash.events.TimerEvent;
	import flash.utils.Dictionary;
	import flash.utils.Timer;   

	public class Shake
	{
		private var timer:Timer;   
		private var deley:Number;   
		private var startX:Number;   
		private var startY:Number;   
		private var para:Number;//震动的参数   
		private var m_contain:Dictionary;   
		private var currentID:String;   
		private var _callBack:Function;
		/**
		 *  
		 * @param deley 频率（周期） 
		 * @param para 振幅
		 * 
		 */
		public function Shake(deley:Number=100,para:Number=5,fun:Function=null) 
		{
			
			m_contain=new Dictionary(true);  
			_callBack=fun;
			this.deley=deley;   
			this.para=para;  
			
			timer=new Timer(deley);   
			timer.addEventListener(TimerEvent.TIMER,onTimer,false,0,true);   
		}   
		
		public function addList(id:String,obj:*):void  
		{   
			m_contain[id]=obj;   
		}   
		private function onTimer(event:TimerEvent):void  
		{   
			moveBy();   
		}   
		public function start(id:String):void  
		{   
			startX=getObject(id).x;   
			startY=getObject(id).y;   
			currentID=id;   
			timer.start();   
		}   
		private function getObject(id:String):*   
		{   
			return m_contain[id];   
		}   
		public function stop():void  
		{   
			timer.stop();   
			getObject(currentID).x=startX;   
			getObject(currentID).y = startY;   
		}   
		public function destory():void  
		{   
			timer.removeEventListener(TimerEvent.TIMER,onTimer);   
		}   
		public function reStart():void  
		{   
			timer=new Timer(deley);   
			timer.addEventListener(TimerEvent.TIMER,onTimer);   
		}   
		
		private function moveBy():void  
		{   
			getObject(currentID).x=startX+Math.random()*para;   
			getObject(currentID).y=startY+Math.random()*para;   
		} 
	}
	
}